package com.qf.web.controller;

import com.qf.pojo.Admin;
import com.qf.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@Slf4j
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("/admin_header.html")
    public String adminHeader(){
        return "admin_header";
    }

    @GetMapping("/admin_main.html")
    public String adminMain(){
        return "admin_main";
    }

    /**
     * 跳转到改密码界面
     *  @return
     */
    @GetMapping("/admin/updateUI.html")
    public ModelAndView updateUI(){
        log.info("AdminController updateUI ......");
        ModelAndView modelAndView = new ModelAndView("admin-repasswd");
        return modelAndView;
    }

    /**
     * 修改密码
     * @param admin
     * @param newPasswd
     * @return
     */
    @PostMapping("/admin/update")
    public ModelAndView update(Admin admin, String newPasswd){
        log.info("AdminController update ......");
        admin.setPassword(newPasswd );
        adminService.updatePassword(admin);
        ModelAndView modelAndView = new ModelAndView("redirect:/admin_main.html");
        modelAndView.addObject("succ","更新成功");
        return modelAndView;
    }

}
